package com.qymerge.manage.base.controller;

import com.google.common.reflect.TypeToken;
import com.qymerge.manage.base.dto.OrganizationalStructureDTO;
import com.qymerge.manage.base.ro.OrganizationalStructureRO;
import com.qymerge.manage.base.service.IOrganizationalStructureService;
import com.qymerge.manage.core.bean.ActionResultRO;
import com.qymerge.manage.core.bean.ParamValue;
import com.qymerge.manage.core.constant.ParamKey;
import com.qymerge.manage.core.enums.StatusTypes;
import com.qymerge.manage.core.utils.CommonUtil;
import com.qymerge.manage.core.utils.GsonUtil;
import com.qymerge.manage.core.utils.OrganizationalStructureUtils;
import com.qymerge.manage.core.utils.PrintCodeUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>Title: IOrganizationalStructureManageController</p>
 * <p>Description: 组织机构管理</p>
 *
 * @author Xiao
 * @version 1.0.0
 * @date 2025/4/23 13:08
 */
@Controller
@RequestMapping(value = "base/ors")
public class IOrganizationalStructureManageController {

    @Autowired
    private IOrganizationalStructureService organizationalStructureService;

    /**
     * 列表
     *
     * @param parameters
     * @return
     */
    @RequestMapping(value = "/list")
    public void sysMenuList(HttpServletRequest request, HttpServletResponse response,
                            @RequestParam(defaultValue = ParamKey.In.dlt_parameters) String parameters,
                            @RequestParam(defaultValue = ParamKey.In.empty) String callback) {
        ParamValue<OrganizationalStructureRO> paramValue = GsonUtil.json2T(parameters, new TypeToken<ParamValue<OrganizationalStructureRO>>() {
        }.getType());

        OrganizationalStructureRO roSearch = paramValue.getData();
        List<OrganizationalStructureRO> organizationalList = organizationalStructureService.organizationalList(roSearch);

        //封装到 OrganizationalStructureDTO 中
        List<OrganizationalStructureDTO> organizationalStructureDTOList = new ArrayList<OrganizationalStructureDTO>();
        if (!CollectionUtils.isEmpty(organizationalList)) {
            for (OrganizationalStructureRO ro : organizationalList) {
                OrganizationalStructureDTO dto = new OrganizationalStructureDTO();
                dto.setId(CommonUtil.getLongNotNull(ro.getId()));
                dto.setParent_id(CommonUtil.getLongNotNull(ro.getParent_id()));
                dto.setCompany(CommonUtil.getStringNotNullValue(ro.getCompany()));
                dto.setCenter(CommonUtil.getStringNotNullValue(ro.getCenter()));
                dto.setNoeLevel(CommonUtil.getIntegerNotNull(ro.getNoeLevel()));
                dto.setDepart(CommonUtil.getStringNotNullValue(ro.getDepart()));
                dto.setSort(CommonUtil.getLongNotNull(ro.getSort()));
                organizationalStructureDTOList.add(dto);
            }
        }

        PrintCodeUtils.printSuccess(response, OrganizationalStructureUtils.formatMenuJson(organizationalStructureDTOList), callback);
    }

    /**
     * 详情
     *
     * @param parameters
     * @param callback
     * @return
     */
    @RequestMapping(value = "/getById")
    public void getMenuById(HttpServletRequest request, HttpServletResponse response,
                            @RequestParam(defaultValue = ParamKey.In.dlt_parameters) String parameters,
                            @RequestParam(defaultValue = ParamKey.In.empty) String callback) {
        ParamValue<OrganizationalStructureRO> paramValue = GsonUtil.json2T(parameters, new TypeToken<ParamValue<OrganizationalStructureRO>>() {
        }.getType());
        OrganizationalStructureRO ro = paramValue.getData();

        OrganizationalStructureRO result = organizationalStructureService.getOrganizational(ro);
        if (null != result) {
            PrintCodeUtils.printSuccess(response, result, callback);
        } else {
            PrintCodeUtils.printError(response, "详情获取失败");
        }
    }

    /**
     * 新增
     *
     * @param parameters
     * @param callback
     * @return
     */
    @RequestMapping(value = "/save")
    public void saveSysMenu(HttpServletRequest request, HttpServletResponse response,

                            @RequestParam(defaultValue = ParamKey.In.dlt_parameters) String parameters,
                            @RequestParam(defaultValue = ParamKey.In.empty) String callback) {
        ParamValue<OrganizationalStructureRO> paramValue = GsonUtil.json2T(parameters, new TypeToken<ParamValue<OrganizationalStructureRO>>() {
        }.getType());
        OrganizationalStructureRO ro = paramValue.getData();

        ro.setCreate_user_id(-1L);
        ro.setCreate_time(System.currentTimeMillis());
        ro.setStatus(StatusTypes.OnLine.getStatus());
        ActionResultRO data = organizationalStructureService.saveOrganizational(ro);
        PrintCodeUtils.printSuccess(response, data, callback);
    }

    /**
     * 修改
     *
     * @param parameters
     * @return
     */
    @RequestMapping(value = "/edit")
    public void editSysMenu(HttpServletRequest request, HttpServletResponse response,

                            @RequestParam(defaultValue = ParamKey.In.dlt_parameters) String parameters,
                            @RequestParam(defaultValue = ParamKey.In.empty) String callback) {
        ParamValue<OrganizationalStructureRO> paramValue = GsonUtil.json2T(parameters, new TypeToken<ParamValue<OrganizationalStructureRO>>() {
        }.getType());
        OrganizationalStructureRO ro = paramValue.getData();

        ro.setUpdate_user_id(-1L);
        ro.setUpdate_time(System.currentTimeMillis());
        ro.setStatus(StatusTypes.OnLine.getStatus());
        ActionResultRO data = organizationalStructureService.editOrganizational(ro);
        PrintCodeUtils.printSuccess(response, data, callback);
    }

    /**
     * 删除
     *
     * @param parameters
     * @param callback
     * @return
     */
    @RequestMapping(value = "/del")
    public void delSysMenu(HttpServletRequest request, HttpServletResponse response,

                           @RequestParam(defaultValue = ParamKey.In.dlt_parameters) String parameters,
                           @RequestParam(defaultValue = ParamKey.In.empty) String callback) {
        ParamValue<OrganizationalStructureRO> paramValue = GsonUtil.json2T(parameters, new TypeToken<ParamValue<OrganizationalStructureRO>>() {
        }.getType());
        OrganizationalStructureRO ro = paramValue.getData();

        ActionResultRO data = organizationalStructureService.delOrganizational(ro.getId());
        PrintCodeUtils.print(response, data, callback);
    }

}
